package com.admin.business.voteStatuteImg;

import com.admin.business.voteComment.VoteCommentDao;
import com.admin.business.voteEssentialInformation.VoteEssentialInformationDao;
import com.admin.business.voteVotingRecords.VoteVotingRecordsDao;
import com.tiangong.framework.annotation.OpenApi;
import com.tiangong.framework.bo.CDO;
import com.tiangong.framework.bo.Return;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description: 规约文件记录表
 * @create: 2022-07-08 19:04:49
 **/
@Service("voteStatuteImg")
@Slf4j
public class VoteStatuteImgService {

    @Resource
    VoteStatuteImgDao voteStatuteImgDao;

//    上传添加规约文件

    @OpenApi
    public String addUploadTermsVote(CDO addVoteUser) {
        voteStatuteImgDao.addVoteStatuteImg(addVoteUser);
        String voteStatuteId = addVoteUser.getString("id");
        return voteStatuteId;
    }

    @OpenApi
    public Return updateUploadTermsVote(CDO cdoRequest) {

        int i = voteStatuteImgDao.updateUploadTermsVote(cdoRequest);
        if (i == 0) {
            return Return.valueOf(-2, "操作失败");
        }
        return Return.valueOf(cdoRequest);
    }


    @Resource
    VoteEssentialInformationDao voteEssentialInformationDao;



    @Resource
    VoteCommentDao voteCommentDao;

    //    第四步业主端操作 获取文件信息和评论数量 -获取文件id 查询
    @OpenApi
    public Return getFourFileInformationComments(CDO cdoRequest) {
        Long id = cdoRequest.getLong("id");
        if (id == null) {
            return Return.valueOf(-2, "未获取到文件id");
        }

        CDO voteStatuteImgById = voteStatuteImgDao.getVoteStatuteImgById(Math.toIntExact(id));
        if (voteStatuteImgById != null) {
            CDO votConent = new CDO();
            votConent.set("statute_id", id);
            List<CDO> voteCommentList = voteCommentDao.getCommentPersonnelInformation(votConent);
            voteStatuteImgById.set("voteComment", voteCommentList);

//            获取基本信息
            CDO voteEsenInfo = new CDO();
            voteEsenInfo.set("id", voteStatuteImgById.getLong("essential_information_id"));
            CDO voteEssentialInformation = voteEssentialInformationDao.getVoteEssentialInformation(voteEsenInfo);
            voteStatuteImgById.set("voteEsenInfo", voteEssentialInformation);


        }
        return Return.valueOf(voteStatuteImgById);
    }



    @Resource
    VoteVotingRecordsDao voteVotingRecordsDao;





    /**
     * 添加规约文件记录表,获取id数据
     *
     * @param cdoRequest - id:
     *                   - user_id:发起人id
     *                   - essential_information_id:基本信息id
     *                   - statute_img:规约文件路径
     *                   - statute_state:规约文件状态：1、业主规约，2、议事规则，3、召开业主大会方案，4、认证委员方案，5、其他文件
     *                   - statute_edition:版本号
     *                   - statute_agree:同意人数
     *                   - statute_disagree:不同意人数
     *                   - begin_time:创建时间
     *                   - update_time:更新时间
     *                   - state:删除，0正常1删除
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
//    每个文件只能上传·一次 最多每个一件文件
    @OpenApi
    public Return addVoteStatuteImg(CDO cdoRequest) {
//     现在可以设置多个文件从新上传
        voteStatuteImgDao.addVoteStatuteImg(cdoRequest);
        CDO cdoResult = new CDO();
        if (cdoRequest.containsKey("id")) {
            cdoResult.set("id", cdoRequest.get("id"));
        }
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据id，更新规约文件记录表数据
     *
     * @param cdoRequest - id:
     *                   - user_id:发起人id
     *                   - essential_information_id:基本信息id
     *                   - statute_img:规约文件路径
     *                   - statute_state:规约文件状态：1、业主规约，2、议事规则，3、召开业主大会方案，4、认证委员方案，5、其他文件
     *                   - statute_edition:版本号
     *                   - statute_agree:同意人数
     *                   - statute_disagree:不同意人数
     *                   - begin_time:创建时间
     *                   - update_time:更新时间
     *                   - state:删除，0正常1删除
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return updateVoteStatuteImgById(CDO cdoRequest) {
        int affectedRows = voteStatuteImgDao.updateVoteStatuteImgById(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据id，删除规约文件记录表数据
     *
     * @param cdoRequest - id:
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return deleteVoteStatuteImgById(CDO cdoRequest) {
        int affectedRows = voteStatuteImgDao.deleteVoteStatuteImgById(cdoRequest.getInteger("id"));
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据条件，删除规约文件记录表数据
     *
     * @param cdoRequest
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return deleteVoteStatuteImg(CDO cdoRequest) {
        int affectedRows = voteStatuteImgDao.deleteVoteStatuteImg(cdoRequest);
        CDO cdoResult = new CDO();
        cdoResult.set("affectedRows", affectedRows);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据ID，获取规约文件记录表数据
     *
     * @param cdoRequest - id:
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return getVoteStatuteImgById(CDO cdoRequest) {
        CDO cdoMapperResult = voteStatuteImgDao.getVoteStatuteImgById(cdoRequest.getInteger("id"));
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 根据混合条件，获取规约文件记录表数据
     *
     * @param cdoRequest - id:
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return getVoteStatuteImg(CDO cdoRequest) {
        CDO cdoMapperResult = voteStatuteImgDao.getVoteStatuteImg(cdoRequest);
        return Return.valueOf(cdoMapperResult);
    }

    /**
     * 根据非空条件，获取规约文件记录表数据列表
     *
     * @param cdoRequest
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return getVoteStatuteImgList(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = voteStatuteImgDao.getVoteStatuteImgList(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
//        int total = voteStatuteImgDao.getVoteStatuteImgListCount(cdoRequest);
//        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }

    /**
     * 根据混合条件，获取规约文件记录表数据列表
     *
     * @param cdoRequest - id:
     * @return
     * @author ht
     * @create: 2022-07-08 19:04:49
     */
    @OpenApi
    public Return getVoteStatuteImgListByCondition(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        List<CDO> cdoMapperResultList = voteStatuteImgDao.getVoteStatuteImgListByCondition(cdoRequest);
        boolean isEndPage = false;
        if (cdoMapperResultList.size() < recordSize) {
            isEndPage = true;
        }
        CDO cdoResult = new CDO();
        cdoResult.set("isEndPage", isEndPage);
        cdoResult.set("datalist", cdoMapperResultList);
//        int total = voteStatuteImgDao.getVoteStatuteImgListByConditionCount(cdoRequest);
//        cdoResult.set("total", total);
        return Return.valueOf(cdoResult);
    }

    @OpenApi
    public Return getVoteStatuteImgNewStateList(CDO cdoRequest) {
        Integer pageIndex = cdoRequest.containsKey("pageIndex") ? cdoRequest.getInteger("pageIndex") : 0;
        int recordSize = cdoRequest.containsKey("recordSize") ? cdoRequest.getInteger("recordSize") : 10;
        cdoRequest.set("recordFrom", pageIndex * recordSize);
        cdoRequest.set("recordSize", recordSize);
        //        全部信息
        List<CDO> voteStatuteImgList = voteStatuteImgDao.getVoteStatuteImgList(cdoRequest);
//        最新信息
        List<CDO> cdoMapperResultList = voteStatuteImgDao.getVoteStatuteImgNewStateList(cdoRequest);
        for (CDO cdo : cdoMapperResultList) {
            cdoRequest.set("statute_state", cdo.getLong("statute_state"));
            //       以外信息
            List<CDO> voteStatuteImgBeyondInfo = voteStatuteImgDao.getVoteStatuteImgBeyondInfo(cdoRequest);
            cdo.set("beyondInfo", voteStatuteImgBeyondInfo);

            CDO statuteImgList = new CDO();
            statuteImgList.set("id", cdo.getLong("id"));
            statuteImgList.set("essential_information_id", cdo.getLong("essential_information_id"));
            statuteImgList.set("user_id", cdoRequest.getLong("user_id"));
            CDO voteStatuteImgCommissioner = this.getVoteStatuteImgCommissioner(statuteImgList);
            cdo.set("stingState", voteStatuteImgCommissioner);


        }


        CDO cdoResult = new CDO();
        cdoResult.set("latestInfo", cdoMapperResultList);
        cdoResult.set("allInfo", voteStatuteImgList);
        return Return.valueOf(cdoResult);
    }


    //    根据委员人数判断同意过半
    @OpenApi
    public CDO getVoteStatuteImgCommissioner(CDO statuteImgList) {
        boolean stingState = true;
        boolean userVote = true;
        boolean operate = true;
        String id = statuteImgList.getString("essential_information_id");

        CDO voteEssentialInformationById = voteEssentialInformationDao.getVoteEssentialInformationById(Integer.valueOf(id));
        if (voteEssentialInformationById != null) {
            Long preparations_number = voteEssentialInformationById.getLong("preparations_number");

            Long preparNumber = preparations_number / 2;

            CDO voteStatuteImgById = voteStatuteImgDao.getVoteStatuteImg(statuteImgList);
            Long statute_agree = voteStatuteImgById.getLong("statute_agree");//同意人数数
            if (statute_agree <= preparNumber) {//同意人数达到一半就变灰
                stingState = false;//文件变灰
            }
            CDO votingRes = new CDO();
            votingRes.set("statute_id", statuteImgList.getString("id"));
            votingRes.set("user_id", statuteImgList.getString("user_id"));
//        投票人员投票不可以显示
            int voteVotingRecordsCount = voteVotingRecordsDao.getVoteVotingRecordsCount(votingRes);
            if (voteVotingRecordsCount > 0) {
                operate=true;
                userVote = false;
            }else {
                operate=false;
                userVote=true;
            }

        }
        CDO voteInfo = new CDO();
        voteInfo.set("state", stingState);
        voteInfo.set("userVote", userVote);
        voteInfo.set("operate", operate);
        return voteInfo;
    }


}
